import Vue from 'vue';
import Vuex from 'vuex';
import Auth from 'src/api/auth';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        /**
         * 全局loading
         */
        loading: 0,
        /**
         * 用户信息
         * @returns {*}
         */
        get userInfo() {
            return Auth.getUserInfo();
        },
        /**
         * 访问token
         */
        get accessToken() {
            return Auth.getAccessToken();
        },
        /**
         * 整个token对象
         */
        get token() {
            return Auth.getToken();
        }
    },
    mutations: {
        changeLoading: (state, count) => {
            state.loading += count;
        },
        resetLoading: (state) => {
            state.loading = 0;
        }
    },
    actions: {
        /**
         * loading增加
         */
        loadingUp({ commit }) {
            commit('changeLoading', 1);
        },
        /**
         * loading减少
         */
        loadingDown({ commit }) {
            commit('changeLoading', -1);
        }
    }
});
